package com.example.child_protection.service.impl;

import com.example.child_protection.dao.DepartmentDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.DepartmentInsertRequestDTO;
import com.example.child_protection.dto.request.update.DepartmentUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.DepartmentModel;
import com.example.child_protection.service.DepartmentManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 儿童档案管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class DepartmentManageServiceImpl extends MyBaseService implements DepartmentManageService {

    @Autowired
    public DepartmentDao departmentDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(DepartmentInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.DEPARTMENT_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<DepartmentInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(DepartmentInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                DepartmentModel model = new DepartmentModel();
                BeanUtils.copyProperties(requestDTO,model);
                departmentDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(DepartmentUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.DEPARTMENT_UPDATE,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<DepartmentUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(DepartmentUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                DepartmentModel model = new DepartmentModel();
                BeanUtils.copyProperties(requestDTO,model);
                departmentDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service (ServiceActionEnums.DEPARTMENT_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                DepartmentModel model = new DepartmentModel();
                BeanUtils.copyProperties(requestDTO,model);
                departmentDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

}
